# INTRODUCE THE CLASSES TO BE USED HERE!

init python:
    
    #AUTHOR of Event Class: Peter Thatcher 
    #http://www.valuedlessons.com/2008/04/events-in-python.html
    
    class Event:
        def __init__(self):
            self.handlers = set()

        def handle(self, handler):
            self.handlers.add(handler)
            return self

        def unhandle(self, handler):
            try:
                self.handlers.remove(handler)
            except:
                raise ValueError("Handler is not handling this event, so cannot unhandle it.")
            return self

        def fire(self, *args, **kargs):
            for handler in self.handlers:
                handler(*args, **kargs)

        def getHandlerCount(self):
            return len(self.handlers)

        __iadd__ = handle
        __isub__ = unhandle
        __call__ = fire
        __len__  = getHandlerCount
    
    class Calendar:
        def __init__(self):
            self.current_date = 1
            self.current_month = 1
            self.current_year = 1
            self.current_day = 0 #SUNDAY
            self.days_elapsed = 0
            
            #self.weather = []
            self.events = []
            self.whole_day = []
            
        def advance_day(self):
            
            self.days_elapsed += 1
            
            if(self.current_date >= 28 and self.current_month == 2):
                self.current_month += 1
                self.current_date = 1
            elif(self.current_date >= 30 and (self.current_month == 4 or self.current_month == 6 or self.current_month == 9 or self.current_month == 11)):
                self.current_month += 1
                self.current_date = 1
            elif(self.current_date >= 31):
                self.current_month += 1
                self.current_date = 1
            else:
                self.current_date += 1
            
            if(self.current_month == 13):
                self.current_year += 1
                self.current_month = 1
            
            self.current_day = (self.current_day + 1) % 7
            
        def init_calendar_days(self):
            
            import xml.etree.ElementTree as elementtree
        
            tree = elementtree.parse(renpy.loader.transfn("xml/Calendar.xml"))
            root = tree.getroot()
            
            for entry in root.iter('calendar_entry'):
                fevent = int(entry.find('event').text) #check for events in that day
                #fweather = entry.find('weather').text #checks weather for the day 
                fwholedayevent = int(entry.find('is_whole_day').text) #check if the event is whole day
                #self.weather.append(fweather)
                self.events.append(fevent)
                self.whole_day.append(fwholedayevent)
        
        def has_day_event(self):
            return self.events[self.days_elapsed]
        def is_fullday_event(self):
            return self.whole_day[self.days_elapsed]
        
        
    class Employee:
        def __init__(self, name, age, gender):
            self.name = name
            self.age = age
            self.gender = MALE
            self.description = " "
            self.friendship = 5
            self.unlocked = False
            self.relationship_flag = UNDECIDED
            self.activity = SERVE
            self.unlocked = False
            self.salary = 500
            self.salary_balance = 50 #0-100. 50 is the exact salary. Adjust to lower/increase salary count
            self.salary_adjusted = 0
            self.actual_salary = 503
            
            self.charm = 10
            self.craft = 10
            self.courage = 10
            self.stress = 0
            
            self.serve_stress = 1
            self.cook_stress = 1
            self.perform_stress = 1
            
            self.social_link = 0
            self.event_rel = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
            
            self.pinx = 100
            self.piny = 150
            
            self.emotions = []
        def __str__(self):
            s = "Name: " + self.name
            s += "Age: " + self.age
            s += "Friendship: " + self.friendship
            return s
        def init_emotions(neutral, happy, sad, angry, surprised, blush):
            self.emotions[NEUTRAL] = neutral
            self.emotions[HAPPY] = happy
            self.emotions[SAD] = sad
            self.emotions[ANGRY] = angry
            self.emotions[SURPRISED] = surprised
            self.emotions[BLUSH] = blush
        def add_friendship_pts(self, amount):
            self.friendship += amount
        def raise_social_link(self):
            self.social_link += 1
        def event_check(self):
            if self.social_link > len(self.event_rel):
                return -1
            if self.event_rel[self.social_link] <= self.friendship:
                return self.social_link
            else:
                return -1
        def unlock(self):
            self.activity = REST
            self.unlocked = True
        def set_salary(self, new_salary):
            self.salary = new_salary
            #self.salary_adjusted = int(new_salary * (self.salary_balance-100))
        def adjust_salary_balance(self, new_value):
            self.salary_adjusted = new_value
            
        def get_salary(self):
            #THE 20% RANGE COMPUTATION
            #self.actual_salary = int((self.salary * ( (self.salary_balance - 50.0)/250.0 + 1.0)))
            #THE 10% RANGE COMPUTATION
            self.actual_salary = int((self.salary * ( (self.salary_balance - 50.0)/500.0 + 1.0)))
            print "TAEE"
            #refresh_flag = True
            #renpy.restart_interaction()
            return self.actual_salary
            
        def get_salary_text(self, st, at):
            return Text("%d" % self.get_salary()), .1
            
    class Player:
        def __init__(self, name):
            self.fname = name
            self.lname = "LAST"
            self.gender = MALE
            #initialize attributes
            self.attributes = [10,10,10]
            self.fatigue = 0
            #self.char = DynamicCharacter("name", color=(192, 64, 64, 255))
            self.tutorialprog = []
                
        def __str__(self):
            s = "Name: " + self.name
            s += "Gender: " + self.gender
            s += "Fatigue: " + self.fatigue
            return s
                
        def set_name(self, firstname, lastname):
            self.fname = firstname
            self.lname = lastname
            
        def set_gender(self, gender_code):
            self.gender = gender_code
            
        def inc_attribute(self, attribute_code, increment):
            self.attributes[attribute_code] += increment
            
            if(self.attributes[attribute_code] < MIN_ATTRIBUTE_VALUE):
                self.attributes[attribute_code] = MIN_ATTRIBUTE_VALUE
            elif(self.attributes[attribute_code] > MAX_ATTRIBUTE_VALUE):
                self.attributes[attribute_code] = MAX_ATTRIBUTE_VALUE
            
        def get_attribute(self, attribute_code): #USE THIS WHEN PRINTING ON CONVERSATIONS!
            return self.attributes[attribute_code]
    
    class Item:
        def __init__(self, name, type, upgrade_price, sell_price, day_available, img_url):
            self.name = name
            self.type = type
            self.level = 1
            self.day_available = day_available
            self.upgrade_price = upgrade_price
            self.sell_price = sell_price
            self.active = False
            self.img_url = img_url
            self.sales = 0
        
        def activate_item(self):
            self.active = True
        
        def level_up(self):
            self.level += 1
        

#    class Inventory: #I guess this is the money...
#        def __init__(self):
#            self.items = []

#        def buy_item(self, item):
#            if self.money >= item.cost:
#                self.money -= item.cost
#                self.items.append(item)
#                return True
#            else:
#                return False

#        def earn(self, amount):
#            self.money += amount

#        def has_item(self, item):
#                if item in self.items:
#                    return True
#                else:
#                    return False
#        def load_item_at(self, index):
#            if index >= len(self.items):
#                return -1
#            else:
#                return self.items[index]
            
            
    class Cafe:
        def __init__(self, name):
            self.name = name
            self.stats = [1,1,1,1]
            self.profits = []
            self.expenses = []
            self.income = []
            self.money = 10000
            
            self.main_mission_complete = False
            self.main_mission_req = 0
            self.sub_mission_complete = False
            self.sub_mission_req = 0
            
        def spend_money(self, cost):
            self.money -= cost
            if self.money < 0:
                self.money = 0
                
        def has_enough_money(self, cost):
            if self.money >= cost:
                return True
            else:
                return False
        
        def set_data(self, stat, increase):
            self.stats[stat] += increase
            
    class EventCard:
        def __init__(self, name, type, threshold):
            self.name = name
            self.type = type
            self.active = false
            self.threshold = 0.0
        
            
            
    ############### QUICK CALL CLASSES ################
            
    class incrementRecipePage():
        def __call__(self):
            global recipe_page
            recipe_page += 1
            renpy.restart_interaction()
    
    class decrementRecipePage():
        def __call__(self):
            global recipe_page
            recipe_page -= 1
            renpy.restart_interaction()
    class resetRecipePage():
        def __call__(self):
            global recipe_page
            recipe_page = 1
            renpy.restart_interaction()
    
    def void():
        ## Do something that won't affect the rest of the game
        DummyVariable = 0